package br.com.site.fiolaser.util;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.regex.Pattern;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

public class DoubleConverter implements Converter{  

	public Object getAsObject(FacesContext arg0, UIComponent arg1, String valor) throws ConverterException {  
		if(valor == null || valor.toString().trim().equals("")){  
			return null;  
			//return 0.0d;

		} else {  
			valor = valor.replaceAll(Pattern.quote("."), "");  
			valor = valor.replaceAll(Pattern.quote(","), ".");  
			Double a = Double.valueOf(valor);  
			return a;  
		}  

	}        


	public String getAsString(FacesContext arg0, UIComponent arg1, Object valor) throws ConverterException {  

		if(valor == null || valor.toString().trim().equals("")){  
			//return "0,00";  
			return null;

		} else {  
			DecimalFormat df = new DecimalFormat("###,##0.00");  
			df.setDecimalFormatSymbols( new DecimalFormatSymbols( new Locale( "PT", "BR" )  ) );  

			return df.format( valor );   


		}  
	}   

}  